package com.yz.mybatis.gen;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.alibaba.fastjson.JSONObject;
import com.yz.mybatis.gen.entity.BasisInfo;
import com.yz.mybatis.gen.util.EntityInfoUtil;
import com.yz.mybatis.gen.util.Generator;
import com.yz.mybatis.gen.util.MySqlToJavaUtil;
import lombok.SneakyThrows;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.Date;

/**
 * MyMojo.
 *
 * @author <a href="mailto:yuanz@yxt.com">yuanz</a>
 * @goal gen：表示该插件的服务目标
 * @phase compile：表示该插件的生效周期阶段
 * @date 2022-02-08 15:17
 * @since 1.0.0
 **/
@Mojo(name = "gen")
public class MyMojo extends AbstractMojo {
    
    @Parameter(name = "configurationFile")
    private String configurationFile;
    
    @Parameter(name = "basedir")
    private String basedir;
    
    public void setConfigurationFile(String configurationFile) {
        this.configurationFile = configurationFile;
    }
    
    public void setBasedir(String basedir) {
        this.basedir = basedir;
    }
    
    @SneakyThrows
    public void execute() {
        System.out.println("mybatis:gen插件执行开始");
        Long startTime = System.currentTimeMillis();
        BufferedReader br = null;
        String configJson;
        try {
            System.out.println("project.basedir:" + basedir);
            br = new BufferedReader(new InputStreamReader(new FileInputStream(basedir + configurationFile)
                , "UTF-8"));
            String line = "";
            configJson = "";
            while ((line = br.readLine()) != null) {
                configJson += line;
            }
        } finally {
            br.close();
        }
        System.out.println("configJson:" + configJson);
        BasisInfo bi = JSONObject.parseObject(configJson, BasisInfo.class);
        bi.setCreateTime(new Date().toLocaleString());
        bi.setEntityName(MySqlToJavaUtil.getClassName(bi.getTable()));
        bi.setObjectName(MySqlToJavaUtil.changeToJavaFiled(bi.getTable()));
        bi.setEntityComment(bi.getTable());
        bi.setAgile(new Date().getTime() + "");
        
        try {
            bi = EntityInfoUtil.getInfo(bi);
            String dalFileUrl = bi.getDalFileUrl();
            String serviceFileUrl = bi.getServiceFileUrl();
            String controllerFileUrl = bi.getControllerFileUrl();
            //开始生成文件
            String entityFile = Generator.createEntity(dalFileUrl, bi).toString();
            String entityDTOFile = Generator.createEntityDTO(serviceFileUrl, bi).toString();
            String daoFile = Generator.createDao(dalFileUrl, bi).toString();
            String daoImplFile = Generator.createMapperXml(dalFileUrl, bi).toString();
            String serviceFile = Generator.createService(serviceFileUrl, bi).toString();
            String serviceImplFile = Generator.createServiceImpl(serviceFileUrl, bi).toString();
            String controllerFile = Generator.createController(controllerFileUrl, bi).toString();
            System.out.println(entityFile);
            System.out.println(entityDTOFile);
            System.out.println(daoFile);
            System.out.println(daoImplFile);
            System.out.println(serviceFile);
            System.out.println(serviceImplFile);
            System.out.println(controllerFile);
            System.out.println("mybatis:gen插件执行结束,耗时:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
}
